/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.study.dao;

import com.study.util.DButil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.study.dto.EnrollmentDTO;

/**
 *
 * @author Truong Anh Tuan
 */
public class EnrollmentDAO {

    public EnrollmentDAO() {
    }
    Connection con = null;
    PreparedStatement pst = null;
    private final String SQL_READ_BY_ALL = "SELECT * FROM ENROLLMENTS";
    private final String SQL_READ_BY_ID = "SELECT * FROM ENROLLMENTS WHERE STUDENT_ID = ?";
    private final String SQL_CREATE = "INSERT INTO ENROLLMENTS(STUDENT_ID, COURSE_ID) VALUES(?, ?)";
    private final String SQL_UPDATE = "UPDATE ENROLLMENTS set STUDENT_ID = ?, COURSE_ID = ? WHERE ENROLLMENT_ID = ?";
    private final String SQL_DELETE = "DELETE FROM ENROLLMENTS WHERE ENROLLMENT_ID = ?";

    public ArrayList<EnrollmentDTO> ReadByAll() {
        try {
            ArrayList<EnrollmentDTO> enrollment = new ArrayList<EnrollmentDTO>();
            con = DButil.GetConnection();
            pst = con.prepareStatement(SQL_READ_BY_ALL);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                EnrollmentDTO dto = new EnrollmentDTO();
                dto.setEnrollmentId(rs.getInt(1));
                dto.setStudentId(rs.getString(2));
                dto.setCoursesId(rs.getString(3));
                enrollment.add(dto);
            }
            return enrollment;
        } catch (SQLException ex) {
            Logger.getLogger(EnrollmentDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DButil.closeConnection(con);
        }
    }

    public ArrayList<EnrollmentDTO> ReadByStudentId(String id) {
        try {
            ArrayList<EnrollmentDTO> enrollment = new ArrayList<EnrollmentDTO>();
            con = DButil.GetConnection();
            pst = con.prepareStatement(SQL_READ_BY_ID);
            pst.setString(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                EnrollmentDTO dto = new EnrollmentDTO();
                dto.setEnrollmentId(rs.getInt(1));
                dto.setStudentId(rs.getString(2));
                dto.setCoursesId(rs.getString(3));
                enrollment.add(dto);
            }
            return enrollment;
        } catch (SQLException ex) {
            Logger.getLogger(EnrollmentDAO.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally {
            DButil.closeConnection(con);
        }
    }

    public boolean Create(EnrollmentDTO dto) {
        try {
            con = DButil.GetConnection();
            pst = con.prepareStatement(SQL_CREATE);
            pst.setString(1, dto.getStudentId());
            pst.setString(2, dto.getCoursesId());
            if (pst.executeUpdate() > 0) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CoursesDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            DButil.closeConnection(con);
        }

    }

    public boolean Create(String id, String courseId) {
        try {
            con = DButil.GetConnection();
            pst = con.prepareStatement(SQL_CREATE);
            pst.setString(1, id);
            pst.setString(2, courseId);
            if (pst.executeUpdate() > 0) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CoursesDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            DButil.closeConnection(con);
        }

    }

    public boolean Update(EnrollmentDTO dto) {
        try {
            con = DButil.GetConnection();
            pst = con.prepareStatement(SQL_UPDATE);
            pst.setString(1, dto.getStudentId());
            pst.setString(2, dto.getCoursesId());
            if (pst.executeUpdate() > 0) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CoursesDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            DButil.closeConnection(con);
        }
    }

    public boolean Delete(String id) {
        try {
            con = DButil.GetConnection();
            pst = con.prepareStatement(SQL_DELETE);
            pst.setString(1, id);
            if (pst.executeUpdate() > 0) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CoursesDAO.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } finally {
            DButil.closeConnection(con);
        }
    }
}
